package com.muhuahealth.controller;

import com.muhuahealth.entity.BridgeEntity;
import com.muhuahealth.service.BridgeService;
import com.muhuahealth.utils.PageQueryUtil;
import com.muhuahealth.utils.PageResult;
import com.muhuahealth.utils.ServerResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * @author GSK
 * @version 1.0
 * @date 2021/1/24 17:04
 */
@Log4j2
@Controller
@RequestMapping("/admin/bridge")
public class BridgeController {

    @Autowired
    BridgeService bridgeService;

    /**
     * 跳转到桥梁页面
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String bridgePage(HttpServletRequest request){
        request.getSession().setAttribute("path","Basic_data_bridge");
        log.info("跳转到桥梁页面");
        return "/Basic_data_bridge";
    }

    /**
     * 桥梁数据展示列表
     * @param params
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ServerResponse bridgeList(@RequestParam Map<String,Object> params){

        //判断分页查询是否正常，当前页码以及每页条数是否为空
        if(StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
            return ServerResponse.createError("参数异常！");
        }
        //分页查询的参数page、limit
        PageQueryUtil queryUtil = new PageQueryUtil(params);
        //分页的工具类
        PageResult listPage = bridgeService.getBridgeListPage(queryUtil);
        return ServerResponse.createSuccess("success",listPage);
    }

    /**
     *
     * @param bridgeEntity
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse save(BridgeEntity bridgeEntity){

        log.info("开始插入数据");
        if(StringUtils.isEmpty(bridgeEntity.getBridgeName())
            || StringUtils.isEmpty(bridgeEntity.getStartName())
            || StringUtils.isEmpty(bridgeEntity.getEndName())
            || StringUtils.isEmpty(bridgeEntity.getStreet())
            || StringUtils.isEmpty(bridgeEntity.getBuildYear())
            || StringUtils.isEmpty(bridgeEntity.getBridgeAttribute())
            || StringUtils.isEmpty(bridgeEntity.getBridgeTexture())
            || StringUtils.isEmpty(bridgeEntity.getOwnership())){
            return ServerResponse.createError("参数异常");
        }
        return bridgeService.saveBridgeInfo(bridgeEntity);
    }

    /**
     * 更新桥梁信息
     * @param bridgeEntity
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse update(BridgeEntity bridgeEntity){

        log.info("开始修改数据");
        if(StringUtils.isEmpty(bridgeEntity.getBridgeName())
                || StringUtils.isEmpty(bridgeEntity.getStartName())
                || StringUtils.isEmpty(bridgeEntity.getEndName())
                || StringUtils.isEmpty(bridgeEntity.getStreet())
                || StringUtils.isEmpty(bridgeEntity.getBuildYear())
                || StringUtils.isEmpty(bridgeEntity.getBridgeAttribute())
                || StringUtils.isEmpty(bridgeEntity.getBridgeTexture())
                || StringUtils.isEmpty(bridgeEntity.getOwnership())){
            return ServerResponse.createError("参数异常");
        }
        return bridgeService.updateBridgeInfo(bridgeEntity);
    }

    /**
     * 删除桥梁信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse delete(@RequestParam("id") Integer id){

        log.info("开始删除数据");
        if(bridgeService.deleteById(id)){
            return ServerResponse.createSuccess();
        }
        return ServerResponse.createError("删除失败！");
    }

    /**
     * 桥梁数据查询
     * @param bridgeEntity
     * @return
     */
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse research(BridgeEntity bridgeEntity){

        log.info("开始查询数据");
        if(Objects.isNull(bridgeEntity.getId())
                || StringUtils.isEmpty(bridgeEntity.getBridgeName())
                || StringUtils.isEmpty(bridgeEntity.getStartName())
                || StringUtils.isEmpty(bridgeEntity.getEndName())
                || StringUtils.isEmpty(bridgeEntity.getStreet())
                || StringUtils.isEmpty(bridgeEntity.getBuildYear())
                || StringUtils.isEmpty(bridgeEntity.getBridgeAttribute())
                || StringUtils.isEmpty(bridgeEntity.getBridgeTexture())
                || StringUtils.isEmpty(bridgeEntity.getOwnership())){
            return ServerResponse.createError("参数异常");
        }
        return bridgeService.researchBridgeInfo(bridgeEntity);
    }
}
